package cn.iocoder.yudao.module.scenic.controller.app.spot;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.AppSpotRespVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.SpotPageReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.SpotRespVO;
import cn.iocoder.yudao.module.scenic.controller.app.spot.vo.AppSpotNearbyReqVO;
import cn.iocoder.yudao.module.scenic.controller.app.spot.vo.SpotGatherRespVO;
import cn.iocoder.yudao.module.scenic.dal.dataobject.spot.SpotDO;
import cn.iocoder.yudao.module.scenic.service.spot.SpotService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "小程序 - 实践点")
@RestController
@RequestMapping("/scenic/spot")
@Validated
public class AppSpotController {

    @Resource
    private SpotService spotService;


    @GetMapping("/get")
    @Operation(summary = "获得实践点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppSpotRespVO> getSpot(@RequestParam("id") Long id, @RequestParam("openId") String openId) {
        AppSpotRespVO respVO = spotService.getAppSpot(id,openId);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得实践点分页")
    public CommonResult<PageResult<SpotRespVO>> getSpotPage(@Valid SpotPageReqVO pageReqVO) {
        PageResult<SpotDO> pageResult = spotService.getSpotPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpotRespVO.class));
    }



    @GetMapping("/nearby")
    @Operation(summary = "获得附近的实践点")
    @Parameter(name = "lnt", description = "经度", required = true, example = "1024")
    @Parameter(name = "lat", description = "纬度", required = true, example = "1024")
    @PermitAll // 无需登录， 内部校验实现
    public CommonResult<List<SpotRespVO>> nearby(@Valid AppSpotNearbyReqVO spotNearbyReqVO) {
        return spotService.nearby(spotNearbyReqVO);
    }


    @GetMapping("/gather")
    @Operation(summary = "按组织架构统计实践点数量")
    @Parameter(name = "level", description = "层级", required = true, example = "0--代表浙江省 1-- 代表地级市 2-- 代表区县")
    @Parameter(name = "deptId", description = "组织Id", required = true, example = "100表示浙江")
    @PermitAll // 无需登录， 内部校验实现
    public CommonResult<List<SpotGatherRespVO>> gather(@RequestParam("level") String level,@RequestParam("deptId") Long deptId) {
        return success(BeanUtils.toBean(spotService.gather(level,deptId),SpotGatherRespVO.class));
    }

    @GetMapping("/list-by-dept-id")
    @Operation(summary = "查询最下一级组织结构的所有实践点")
    @PermitAll // 无需登录， 内部校验实现
    @Parameter(name = "deptId", description = "组织机构id", required = true, example = "1")
    public CommonResult<List<SpotRespVO>> listByDeptId(@RequestParam("deptId") String deptId) {
        return success(spotService.listByDeptId(deptId));
    }


    @GetMapping("/search")
    @Operation(summary = "搜索实践点")
    @Parameter(name = "name", description = "名称", required = true, example = "abc")
    @PermitAll // 无需登录， 内部校验实现
    public CommonResult<List<SpotRespVO>> search(@RequestParam("name") String name) {
        return success(BeanUtils.toBean(spotService.listByName(name),SpotRespVO.class));
    }











}